package com.zb.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zb.dao.base.OmsCommonDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.base.OmsCommonParam;
import com.zb.entity.base.OmsMetric;
import com.zb.entity.exception.OmsDaoException;
import com.zb.service.base.MetricService;
import com.zb.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class MetricServiceImpl extends BaseService implements MetricService{
	@Autowired
	@Qualifier("base")
	private OmsCommonDao<OmsMetric> omsCommonDaoImpl;
	
	@Override
	public   JsonCommon<?> add(OmsMetric metric) 
	{
		try 
		{
			omsCommonDaoImpl.add(metric);
			return this.buildSuccessJsonCommon(metric);
		} catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> update(OmsMetric metric) 
	{
		if (StringUtils.isEmpty(metric.getMetric_type()))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.update(metric);
			return this.buildSuccessJsonCommon(metric);
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> delete(String metric_type) {
		if (StringUtils.isEmpty(metric_type))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try {
			omsCommonDaoImpl.deleteById(metric_type, OmsMetric.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> findById(String metric_type) {
		if (StringUtils.isEmpty(metric_type))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsMetric metric = new OmsMetric();
			metric.setMetric_type(metric_type);
			OmsMetric op = omsCommonDaoImpl.findRecordByPK(metric);
			return this.buildSuccessJsonCommon(op);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsMetric metric) 
	{
		try 
		{
			List<OmsMetric> lst = omsCommonDaoImpl.pagingFind(param, metric);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = omsCommonDaoImpl.queryTotalByObject(metric);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

}
